﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PictureBoard4.Shape;
using System.Runtime.InteropServices;

namespace PictureBoard4
{
    public partial class Frm1 : Form
    {
        public Frm1()
        {
            InitializeComponent();
        }
        ManageDraw Manage = null;
        CarveImage image = new CarveImage();
        private void Frm1_Load(object sender, EventArgs e)
        {
            Manage = new ManageDraw(this.CreateGraphics(), this.Size);
            Tool.ToolStyle = ToolStyle.Line;
            Tool.SColor = Color.Red;
            Tool.BackColor = this.BackColor;
           
        }
        private void ptb_MouseUp(object sender, MouseEventArgs e)
        {
            Manage.End(e.Location);
        }
        private void ptb_MouseMove(object sender, MouseEventArgs e)
        {
            Manage.Process(e.Location);
        }
        private void ptb_MouseDown(object sender, MouseEventArgs e)
        {
            if(fill){

                Graphics vGraphics = Graphics.FromHwnd(Handle);
                IntPtr vDC = vGraphics.GetHdc();
                IntPtr vBrush = CreateSolidBrush(ColorTranslator.ToWin32(Tool.SColor));
                IntPtr vPreviouseBrush = SelectObject(vDC, vBrush);
                ExtFloodFill(vDC,e.Location.X, e.Location.Y, GetPixel(vDC, e.Location.X, e.Location.Y), FLOODFILLSURFACE);
                SelectObject(vDC, vPreviouseBrush);
                DeleteObject(vBrush);
                vGraphics.ReleaseHdc(vDC);
                Manage = new ManageDraw(this.CreateGraphics(),this.Size);
                Bitmap map = new Bitmap(this.Width,this.Height);
                Graphics g = Graphics.FromImage(map);
                g.CopyFromScreen(this.Location,new Point(-8,-30),this.Size);
                Manage.GetImage(map);
                return;

            }
            if (Manage.Start(e.Location))
            {
                this.Cursor = Cursors.Hand;
            }
            else
            {
                this.Cursor = Cursors.Arrow;
            }
        }
        //截取
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Image;
            fill = false;
        }
        //直线
        private void tslLine_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Line;
            fill = false;
        }
        private void Frm1_Paint(object sender, PaintEventArgs e)
        {
            Manage.Draw();
        }
        //椭圆
        private void tslEllipse_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Ellipse;
            fill = false;
        }
        //铅笔
        private void tslPen_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Pencil;
            fill = false;
        }
        //矩形
        private void tslRectangles_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Rectangles;
            fill = false;
        }
        //三角形
        private void tslTriangle_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Triangle;
            fill = false;
        }
        //橡皮擦
        private void tslEraser_Click(object sender, EventArgs e)
        {
            Tool.ToolStyle = ToolStyle.Eraser;
        }
        private void Frm1_SizeChanged(object sender, EventArgs e)
        {
            //Manage = new ManageDraw(this.CreateGraphics(), this.Size);
        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //DialogResult re = MessageBox.Show("是否退出", "提示框", MessageBoxButtons.OKCancel);
            //if (re == DialogResult.OK)
            //{
                //Application.Exit();
                this.Close();
            //}
        }
        private void 读取ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            Manage.Read(openFileDialog1.FileName);
        }
        //选择颜色
        private void ColorSelect_Click(object sender, EventArgs e)
        {
              colorDialog1.ShowDialog();
              Tool.SColor = colorDialog1.Color;
              this.panel1.BackColor=colorDialog1.Color;
        }
        private void 保存ToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            DialogResult re = MessageBox.Show("是否保存到图片‘123.jpg’中(默认保存在D盘！！！)", "提示框", MessageBoxButtons.OKCancel);
            if (re == DialogResult.OK)
            {
                Manage.Save("D:\\123.jpg");
            }       
        }
        private void numer_ValueChanged(object sender, EventArgs e)
        {
            Tool.SWidth = (int)this.numer.Value;
        }
        #region 填充
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateSolidBrush(int crColor);
        [DllImport("gdi32.dll")]
        public static extern bool ExtFloodFill(IntPtr hdc, int nXStart, int nYStart,
            int crColor, uint fuFillType);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern int GetPixel(IntPtr hdc, int x, int y);
        public static uint FLOODFILLBORDER = 0;
        public static uint FLOODFILLSURFACE = 1;
        bool fill = false;

        private void tslFill_Click(object sender, EventArgs e)
        {
           fill = true;
        }
        #endregion
    }
}
